package com.liukai.novel_info.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.liukai.novel_commons.entities.NovelChapter;
import com.liukai.novel_commons.utils.Constans;
import com.liukai.novel_info.R;

import java.util.List;

public class ReadLeftAdapter extends RecyclerView.Adapter<ReadLeftAdapter.ReadLeftViewHolder> {

    private List<NovelChapter> mMuluChapter;
    private Context mContext;

    public ReadLeftAdapter(){
        mMuluChapter = Constans.getMuluChapter();
    }

    public ReadLeftAdapter(Context context){
        this();
        mContext = context;
    }

    @NonNull
    @Override
    public ReadLeftViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_mulu, null);
        return new ReadLeftViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ReadLeftViewHolder holder, int position) {
        final NovelChapter novelChapter = mMuluChapter.get(position);
        holder.mChapterName.setText(novelChapter.getChapterName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "点击", Toast.LENGTH_SHORT).show();
                mOnChapterListener.onChapterClick(novelChapter);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mMuluChapter.size();
    }

    class ReadLeftViewHolder extends RecyclerView.ViewHolder{

        public TextView mChapterName;

        public ReadLeftViewHolder(@NonNull View itemView) {
            super(itemView);

            mChapterName = itemView.findViewById(R.id.chapter_name);
        }
    }

    public interface OnChapterListener{
        void onChapterClick(NovelChapter novelChapter);
    }

    private OnChapterListener mOnChapterListener;

    public void setOnChapterListener(OnChapterListener onChapterListener) {
        mOnChapterListener = onChapterListener;
    }
}
